package com.css.dzj.dzjwgr.modules.boards.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjwgr.common.audit.service.SysAuditService;
import com.css.dzj.dzjwgr.common.util.BeanUtil;
import com.css.dzj.dzjwgr.common.util.Constant;
import com.css.dzj.dzjwgr.common.util.R;
import com.css.dzj.dzjwgr.modules.boards.entity.BoardsEntity;
import com.css.dzj.dzjwgr.modules.boards.service.BoardService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 宣传板Controller
 *
 * @author wt
 * @date 2020\3\3 0003
 */

@RestController
@RequestMapping("/board")
public class BoardController {

    @Autowired
    private BoardService boardService;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * @description 列表
     * @author wt
     * @date 2020/5/22 14:43
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<BoardsEntity> boardResults = boardService.queryPage(pageRequest);
            s = JSON.toJSONString(boardResults);
            sysAuditService.saveSuccessAudit("宣传板列表查询成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
            return R.ok().put("data", boardResults);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板列表查询失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }


    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id) {
        BoardsEntity boardsEntity = boardService.findById(id);
        String s = JSON.toJSONString(boardsEntity);
        sysAuditService.saveSuccessAudit("宣传板信息查找成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("boardsEntity", boardsEntity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody BoardsEntity boardsEntity) {
        String s = "";
        try {
            if (StringUtils.isEmpty(boardsEntity)) {
                return R.error("宣传板信息不能为空");
            }
            boardService.add(boardsEntity);
            s = JSON.toJSONString(boardsEntity);
            sysAuditService.saveSuccessAudit("宣传板保存成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板保存失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 新建发布
     */
    @PostMapping("/saverelease")
    public R saverelease(@RequestBody BoardsEntity boardsEntity) {
        String s = "";
        try {
            if (StringUtils.isEmpty(boardsEntity)) {
                return R.error("宣传板信息不能为空");
            }
            boardsEntity.setReleaseTime(new Date());
            boardService.saverelease(boardsEntity);
            s = JSON.toJSONString(boardsEntity);
            sysAuditService.saveSuccessAudit("宣传板新建发布成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板新建发布失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody BoardsEntity boardsEntity) {
        String s = "";
        try {
            BoardsEntity oldBoardsEntity = boardService.findById(boardsEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(boardsEntity, oldBoardsEntity);
            oldBoardsEntity.setUpdateTime(new Date());
            boardService.update(boardsEntity);
            s = JSON.toJSONString(boardsEntity);
            sysAuditService.saveSuccessAudit("宣传板修改成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板修改失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id) {
        String s = "";
        try {
            if (boardService.delete(id)) {
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("宣传板删除成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_DELETE, s, request);
                return R.ok("操作成功!");
            } else {
                return R.error("操作失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板删除失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询宣传板信息
     */
    @GetMapping("/xcbList")
    public R getXcbList(@RequestParam String createUser,
                        @RequestParam String title,
                        @RequestParam String createUserunit,
                        @RequestParam String statte,
                        @RequestParam String releaseStartTime,
                        @RequestParam String releaseEndTime,
                        @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<BoardsEntity> boardResults = boardService.getXcbList(createUser, title, createUserunit, statte, releaseStartTime, releaseEndTime, pageRequest);
            s = JSON.toJSONString(boardResults);
            sysAuditService.saveSuccessAudit("多条件查询宣传板成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
            return R.ok().put("data", boardResults);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("宣传板列表查询失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 撤回宣传板信息
     */
    @PostMapping("/recall")
    public R recall(@RequestParam String id) {
        String s = "";
        try {
            BoardsEntity oldBoardsEntity = boardService.findById(id);
            if (!"1".equals(oldBoardsEntity.getStatte())) {
                return R.error("未发布状态，不能撤销!");
            }
            oldBoardsEntity.setStatte("2");
            oldBoardsEntity.setUpdateTime(new Date());
            boardService.update(oldBoardsEntity);
            s = JSON.toJSONString(oldBoardsEntity);
            sysAuditService.saveSuccessAudit("撤回宣传板信息成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_RECALL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("撤回宣传板信息失败", Constant.AUDIT_MODULE_BOARD, Constant.YZ, Constant.AUDIT_TYPE_RECALL, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 发布宣传板信息
     */
    @PostMapping("/release")
    public R release(@RequestBody BoardsEntity boardsEntity) {
        String s = "";
        try {
            BoardsEntity oldBoardsEntity = boardService.findById(boardsEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(boardsEntity, oldBoardsEntity);
            oldBoardsEntity.setStatte("1");
            oldBoardsEntity.setUpdateTime(new Date());
            oldBoardsEntity.setReleaseTime(new Date());
            boardService.update(oldBoardsEntity);
            s = JSON.toJSONString(boardsEntity);
            sysAuditService.saveSuccessAudit("发布宣传板信息成功", Constant.AUDIT_MODULE_BOARD, Constant.PT,  Constant.AUDIT_TYPE_RELEASE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("发布宣传板信息成功", Constant.AUDIT_MODULE_BOARD, Constant.PT, Constant.AUDIT_TYPE_RELEASE, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
